<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/12
 * Time: 16:10
 */
namespace App\Models;

Class Attention extends BaseModel
{

    protected $table = 'attention';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 获取记录数
     */
    public function getCount($where)
    {
        return $this->multiWhere($where)->count();
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 获取关注总量
     */
    public function getAttentionCount($uid){
          $count = $this->getCount(['attuid'=>$uid]);
          return $count;
    }

    /**
     * 添加关注
     */
    public function addAttention($where)
    {
         $loginId = session('loginId');
         $room1 = new Roomnum();
         $where1['num'] = $where['room'];
         $roominof = $room1->getOne($where1);
         $where2['attuid'] = $roominof['uid'];
         $where2['uid']    = session('loginId');
         $isAtten = $this->isAttention($where2);
         if ($isAtten){
             return ['code'=>400,'msg'=>'你已经关注了'];
         }
         if ($loginId==$roominof['uid']){
               return ['code'=>400,'msg'=>'不能关注自己哦'];
         }
         $insert['uid'] = $loginId;
         $insert['attuid'] = $roominof['uid'];
         $res = $this->add($insert);
         if ($res) {
             return ['code'=>200,'msg'=>'关注成功'];
         }
        return ['code'=>400,'msg'=>'关注失败'];
    }

    /**
     * 取消关注
     */
    public function delAttention($where)
    {
        $loginId = session('loginId');
        $room1 = new Roomnum();
        $where1['num'] = $where['room'];
        $roominof = $room1->getOne($where1);
        $where2['attuid'] = $roominof['uid'];
        $where2['uid']    = $loginId;
        $res = $this->del($where2);
        if ($res) {
            return ['code'=>200,'msg'=>'取消成功'];
        }
        return ['code'=>400,'msg'=>'取消失败'];
    }

    /**
     * 判断是否关注
     */
    public function isAttention($where){
          $attention = $this->getOne($where);
          if ($attention){
               return 1;
          }else{
              return 0;
          }
    }
}